package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.RequiredPermission;
import javafx.application.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void saveOrUpdate(Permission permission) {
        if (permission.getId()!=null){
            permissionMapper.updateByPrimaryKey(permission);
        }else {
            permissionMapper.insert(permission);
        }
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission getOne(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> listAll() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }


    public PageResult query(QueryObject qo){
        //查询总条数
        int totalCount = permissionMapper.queryForCount();
        //查询带有分页效果的结果集
        if(totalCount == 0){
            //查不出数据，调用的是pageResult类中的缺参构造器
            return new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }

        List<Permission> data = permissionMapper.queryForList(qo);
        return new PageResult(data,totalCount,qo.getCurrentPage(),qo.getPageSize());
    }

    @Autowired
    private ApplicationContext ctx;
    @Override
    public void reload(){
        //把数据库中所有的权限表达式查询出来
        Set<String> expressions=permissionMapper.selectExpression();
        //加载权限的业务逻辑，要把所有的Controller方法上带有@RequiredPermission 注解的信息保存到数据库中
        //要先拿到所有的Controller，所有的Controller都在spring容器中，上spring容器拿到Controller
        Map<String,Object> map=ctx.getBeansWithAnnotation(Controller.class);
        //Controller在map的value里面，要获取所有的value
        Collection<Object> controllers=map.values();
        for(Object controller:controllers){
            //要拿到每一个Controller中的所有方法
            Method[] methods=controller.getClass().getMethods();
            for(Method method:methods){
                RequiredPermission annotation=method.getAnnotation(RequiredPermission.class);
                //如果方法上有@RequiredPermission，那么把注解里的信息拿出来
                //@RequiredPermission({"部门新增或编辑","department:saveOrUpdate"})
                if (annotation!=null && !expressions.contains(annotation.value()[1])){
                    String name=annotation.value()[0];
                    String expression=annotation.value()[1];
                    Permission permission=new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    //保存到数据库中
                    permissionMapper.insert(permission);
                }
            }
        }
    }
}
